package com.zjb.demo.framework.security;

import com.zjb.demo.common.constant.UserConstants;
import com.zjb.demo.system.service.TokenService;
import org.noear.solon.annotation.Component;
import org.noear.solon.annotation.Inject;
import org.noear.solon.auth.AuthProcessorBase;
import org.noear.solon.core.handle.Context;

import java.util.List;
import java.util.Optional;

/**
 * @author 庄佳彬
 * @since 2023/12/29 12:04
 */
@Component
public class JwtAuthenticationTokenProcessor extends AuthProcessorBase {

    @Inject
    private TokenService tokenService;

    /**
     * 获取用户权限列表
     */
    @Override
    protected List<String> getPermissions() {
        return null;
    }

    /**
     * 获取用户角色列表
     */
    @Override
    protected List<String> getRoles() {
        return null;
    }

    @Override
    public boolean verifyLogined() {
        return tokenService.getLoginUserByToken()
                .map(loginUser -> {
                    var result = tokenService.verifyToken(() -> Optional.of(loginUser));
                    if (result) {
                        Context.current().sessionSet(UserConstants.CURRENT_USER, loginUser);
                    }
                    return result;
                }).orElse(Boolean.FALSE);

    }
}
